package com.cat.dataStructure02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-candies-you-can-get-from-boxes/?envType=problem-list-v2&envId=array
 * @create 2025/11/1 21:00
 * @since JDK17
 */

public class Solution95 {
    public int maxCandies(int[] status, int[] candies, int[][] keys, int[][] containedBoxes, int[] initialBoxes) {
        int ans = 0, n = status.length;
        List<Integer> q = new ArrayList<>();
        Arrays.stream(initialBoxes).forEach(q::add);
        while (!q.isEmpty()) {
            var t = new ArrayList<>(q);
            q = new ArrayList<>();
            for (var x : t) {
                if (status[x] == 0) { // 暂时打不开
                    q.add(x);
                    continue;
                }
                ans += candies[x];
                for (int key : keys[x]) {   // 爽拿钥匙
                    status[key] = 1;
                }
                for (int y : containedBoxes[x]) {   // 爽拿箱子
                    q.add(y);
                }
            }
            if (q.equals(t)) {
                break;
            }
        }

        return ans;
    }
}
